/*
 * Bomb.cpp
 */

#include "Bomb.h"

Bomb::Bomb(Level *level){
	diameter = 0.5;
	this->level = level;
	bombActive = false;
	deactivated = true;
	milisElapsed = 0.0;
	explosion = new Explosion(level);
}

Bomb::~Bomb() {
	delete [] bomb;
	delete explosion;
}

void Bomb::initializeGL(ObjModel *bomb[], GLuint *texture){
	explosion->initializeGL(texture);
	for(int i=0; i<6; i++)
		this->bomb[i] = bomb[i];
}

void Bomb::renderGL()
{
	// we draw 3D model of the bomb for 5 seconds and then we detonate the explosion
	if(milisElapsed < 3000){
		glPushMatrix();
		// move to bomb position
		glTranslatef(pos_x, pos_y, pos_z);
		// draw first 3D model of bomb (start of detonation count down)
		if(milisElapsed < 1*500)
			bomb[0]->renderGL();
		else if(milisElapsed < 2*500)
			bomb[1]->renderGL();
		else if(milisElapsed < 3*500)
			bomb[2]->renderGL();
		else if(milisElapsed < 4*500)
			bomb[3]->renderGL();
		else if(milisElapsed < 5*500)
			bomb[4]->renderGL();
		// draw last 3D model of bomb (end of detonation count down, just before detonation)
		else
			bomb[5]->renderGL();
		glPopMatrix();
	}
	// draw the explosion / detonation (after 5 seconds)
	else{
		bombActive = explosion->renderGL2(); // returns boolean true if explosion is still active
	}
}

void Bomb::update(float milis) {
	// check if bomb position 'is on fire' and if so trigger explosion
	if(level->model[(int)pos_x][(int)pos_y].isOnFire)
		milisElapsed = 3001;
	else
		milisElapsed += milis;
	// if it is time for detonation (after 5 seconds) then update explosion state
	if(milisElapsed > 3000){
		if(!deactivated){
			level->model[(int)pos_x][(int)pos_y].removeModel3dPointer((Model3D*)this);
			deactivated = true;
		}
		explosion->update(milis);
	}
}

bool Bomb::isActive(){
	return bombActive;
}

void Bomb::activate(int x, int y, int size){
	Vertex v(0.49+x*1, 0.5+y*1, 0);
	level->heightCorrection(v);
	pos_x = v.x;
	pos_y = v.y;
	pos_z = v.z+0.25;
	setPosition(pos_x, pos_y, pos_z);
	level->model[x][y].addModel3dPointer((Model3D*)this);
	explosion->setPosition(pos_x, pos_y, pos_z, size);
	milisElapsed = 0.0;
	bombActive = true;
	deactivated = false;
}

Vertex Bomb::getPosition(){
	return Vertex(pos_x, pos_y, pos_z);
}
